/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header: https://192.168.1.224/svn/elancom/product/elancom-platform/trunk/src/main/java/com/elancom/framework/web/BaseController.java 1121 2012-07-11 06:25:51Z xushuai $
 * $Id: BaseController.java 1121 2012-07-11 06:25:51Z xushuai $
 * $Author: xushuai $
 * $Date: 2012-07-11 14:25:51 +0800 (周三, 11 七月 2012) $
 * $Revision: 1121 $
 */
package com.elancom.framework.web;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.elancom.framework.commons.util.StringUtils;

/**
 * base spring
 * 
 * @author elancom
 * @date Apr 9, 2012
 */
public abstract class AbstractController implements Controller {
	@Resource
	protected RequestDelegate requestDelegate;
	
	protected static String VIEW_PATH = "/WEB-INF/jsp";

	/**
	 * 
	 */
	public AbstractController() {
		super();
		System.out.println("控制器组件装配成功:" + getClass().getName());
	}

	/**
	 * @return the requestDelegate
	 */
	public RequestDelegate getRequestDelegate() {
		return requestDelegate;
	}

	/**
	 * @param requestDelegate
	 *            the requestDelegate to set
	 */
	public void setRequestDelegate(RequestDelegate requestDelegate) {
		this.requestDelegate = requestDelegate;
	}

	protected String getParameter(String name) {
		return requestDelegate.getParameter(name);
	}

	protected String getTrimParameter(HttpServletRequest request, String param) {
		final String value = getParameter(param);
		return value != null ? value.trim() : value;
	}

	protected String[] getTrimParameterValues(String name) {
		if (StringUtils.isNotBlank(name)) {
			final String[] values = requestDelegate.getParameterValues(name);
			if (values != null && values.length > 0) {
				for (int i = 0; i < values.length; i++) {
					values[i] = StringUtils.trim(values[i]);
				}
			}
			return values;
		}
		return null;
	}

	/**
	 * 获取参数数组,长度%2总是为0<br>
	 * 形式如下： [键,值,键,值]
	 * 
	 * @param request
	 * @return
	 */
	protected Object[] getTrimParameterArray() {
		return ServletUtils.getTrimParameterArray(getTrimParameterMap());
	}

	protected Map<String, Object> getTrimParameterMap() {
		return ServletUtils.getTrimMap(requestDelegate.getParameterMap());
	}

	protected AbstractController setAddState() {
		getPageState().setAddState();
		return this;
	}

	/**
	 * @return
	 * 
	 */
	private PageState getPageState() {
		PageState state = PageState.get(requestDelegate);
		return state;
	}

	protected AbstractController setUpdateState() {
		getPageState().setUpdateState();
		return this;
	}

	/**
	 * @param arg0
	 * @return
	 * @see com.elancom.framework.web.RequestDelegate#removeAttribute(java.lang.String)
	 */
	protected AbstractController removeAttribute(String arg0) {
		requestDelegate.removeAttribute(arg0);
		return this;
	}

	/**
	 * @param arg0
	 * @param arg1
	 * @return
	 * @see com.elancom.framework.web.RequestDelegate#setAttribute(java.lang.String, java.lang.Object)
	 */
	protected AbstractController setAttribute(String arg0, Object arg1) {
		requestDelegate.setAttribute(arg0, arg1);
		return this;
	}
}
